#include "threadpool.h"

#include <iostream>
#include <thread>
#include <chrono>

class MyTask : public Task 
{
public:
    MyTask(int begin, int end) 
        : begin_(begin)
        , end_(end)
    {

    }

    Any run() 
    {

        //std::cout << " tid : " << std::this_thread::get_id() << " 开始运行... " << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(5));
        //std::cout << " tid : " << std::this_thread::get_id() << " 运行结束... " << std::endl;
        int sum = 0;
        for(int i = begin_; i <= end_; ++i)
            sum += i;
        return sum;
    }
private:
    int begin_;
    int end_;
};

int main()
{

    {
        ThreadPool pool;
        pool.start(4);
        
        Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100));
        Result res2 = pool.submitTask(std::make_shared<MyTask>(101, 200));
        Result res3 = pool.submitTask(std::make_shared<MyTask>(201, 300));
        Result res4 = pool.submitTask(std::make_shared<MyTask>(301, 400));
        Result res5 = pool.submitTask(std::make_shared<MyTask>(401, 500));
        Result res6 = pool.submitTask(std::make_shared<MyTask>(501, 600));
        Result res7 = pool.submitTask(std::make_shared<MyTask>(601, 700));

        int sum1 = res1.get().case_<int>();
        int sum2 = res2.get().case_<int>();
        int sum3 = res3.get().case_<int>();
        int sum4 = res4.get().case_<int>();
        int sum5 = res5.get().case_<int>();
        int sum6 = res6.get().case_<int>();
        int sum7 = res7.get().case_<int>();
        int sum = 0;
        for(int i = 0; i <= 700; ++i)
            sum += i;
        std::cout << sum << "  " << (sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7) << std::endl;
    }
#if 0
    {
        ThreadPool pool;
        pool.setMode(PoolMode::MODE_CACHED);
        pool.start(4);

        Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100));
        Result res2 = pool.submitTask(std::make_shared<MyTask>(101, 200));
        Result res3 = pool.submitTask(std::make_shared<MyTask>(201, 300));
        Result res4 = pool.submitTask(std::make_shared<MyTask>(301, 400));
        Result res5 = pool.submitTask(std::make_shared<MyTask>(401, 500));
        Result res6 = pool.submitTask(std::make_shared<MyTask>(501, 600));
        Result res7 = pool.submitTask(std::make_shared<MyTask>(601, 700));

        int sum1 = res1.get().case_<int>();
        int sum2 = res2.get().case_<int>();
        int sum3 = res3.get().case_<int>();
        int sum4 = res4.get().case_<int>();
        int sum5 = res5.get().case_<int>();
        int sum6 = res6.get().case_<int>();
        int sum7 = res7.get().case_<int>();
        int sum = 0;
        for(int i = 0; i <= 700; ++i)
            sum += i;
        std::cout << sum << "  " << (sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7) << std::endl;
    }
#endif

    std::cout << "main over! " << std::endl;
    return 0;
}